﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace iAgent.Core
{
    interface IFileHelper: IObservable<string>
    {
        bool AddFile(out string FileName);
        void OpenFile(string FileName);
        void ChangeRepositoryPath();
        string Path { get; }
    }
    class FileRepositoryDecorator : DataModel.Core.IFileRepository, IFileHelper
    {
        DataModel.Core.DefaultFileRepository repository;
        public FileRepositoryDecorator()
        {
            observers = new List<IObserver<string>>();
        }
        public string Path
        {
            get
            {
                return Properties.Settings.Default.FileRepositoryPath;
            }
            set
            {
                if (value != Path)
                {
                    Properties.Settings.Default.FileRepositoryPath = value;
                    observers.ForEach(x => x.OnNext(value));
                    CreateRepository();
                }
            }
        }
        private string GetRepositoryPath()
        {
            if (!IsRepositoryFolderSet())
            {
                SetRepositoryFolder();
            }
            return Path;
        }

        private void SetRepositoryFolder()
        {
            using (FolderBrowserDialog dialog = new FolderBrowserDialog())
            {
                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    Path = dialog.SelectedPath;
                    Properties.Settings.Default.Save();
                }
                else
                    observers.ForEach(x=>x.OnError(new System.IO.DirectoryNotFoundException("Необходимо указать путь к папке для сохранения файлов")));  
            }
        }
        private bool InitRepository()
        {
            if (repository == null)
            {
                Path = GetRepositoryPath();
            }
            return repository != null;
        }
        private void CreateRepository()
        {
            repository = new DataModel.Core.DefaultFileRepository(Path);
        }
        public string AddFile(string FileName)
        {
            InitRepository();
            FileName =  repository.AddFile(FileName);
            return System.IO.Path.GetFileName(FileName);  
        }
        private bool IsRepositoryFolderSet()
        {
            return !string.IsNullOrWhiteSpace(Path);
        }
        public bool AddFile(out string FileName)
        {
            FileName = string.Empty;
            if (InitRepository())
            {
                using (OpenFileDialog dialog = new OpenFileDialog())
                {
                    if (dialog.ShowDialog() == DialogResult.OK)
                    {
                        FileName = AddFile(dialog.FileName);
                        return true;
                    }
                }
            }
            return false;
        }


        public void ChangeRepositoryPath()
        {
            if (IsRepositoryFolderSet())
            {
                string OldFolderPath = Path;
                SetRepositoryFolder();
                foreach (var item in System.IO.Directory.GetFiles(OldFolderPath))
                {
                    string FileName = System.IO.Path.GetFileName(item);
                    System.IO.File.Move(item, System.IO.Path.Combine(Path, FileName));   
                }
                observers.ForEach(x => x.OnCompleted()); 
            }
        }

        public void OpenFile(string FileName)
        {
            FileName = System.IO.Path.Combine(Path, FileName);
            System.Diagnostics.Process.Start(FileName);  
        }
        private readonly List<IObserver<string>> observers;
        public IDisposable Subscribe(IObserver<string> observer)
        {
            observers.Add(observer);
            return null;
        }
    }
}
